package com.qxsx.midi.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.Volley;
import com.qxsx.midi.R;
import com.qxsx.midi.activity.study.MidiManDetailsActivity;
import com.qxsx.midi.activity.study.StudyClubDetailActivity;
import com.qxsx.midi.fragment.StudyFragment;
import com.qxsx.midi.model.StudyListItem;
import com.qxsx.midi.utils.BitmapCache;
import com.qxsx.midi.utils.MyNet;

import java.util.ArrayList;
import java.util.List;

/**
 *
 */
public class StudyMainPageListAdapter extends BaseAdapter {

    private Context context;

    private List<StudyListItem> lists = new ArrayList<>();
    private RequestQueue queue;
    private ImageLoader imageLoader;
    public StudyMainPageListAdapter() {
    }

    public StudyMainPageListAdapter(Context context, List<StudyListItem> lists) {
        this.context = context;
        this.lists = lists;
        queue = Volley.newRequestQueue(context);
        imageLoader = new ImageLoader(queue, new BitmapCache());
    }

    @Override
    public int getCount() {
        return lists.size();
    }


    @Override
    public Object getItem(int position) {
        return lists.get(position);
    }


    public long getItemId(int position) {
        return position;
    }

    public void RemoveAll() {
        this.lists.clear();
    }

    public void AddData(List<StudyListItem> lists) {
        for (StudyListItem it : lists) {
            this.lists.add(it);
        }
        notifyDataSetChanged();
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_list_study, null);
            viewHolder.title = (TextView) convertView.findViewById(R.id.study_list_item_title);
            viewHolder.desc = (TextView) convertView.findViewById(R.id.study_list_item_desc);
            viewHolder.iv_myexam = (NetworkImageView) convertView.findViewById(R.id.iv_myexam);
            viewHolder.tv_name = (TextView) convertView.findViewById(R.id.study_list_item_name);
            viewHolder.tv_city = (TextView) convertView.findViewById(R.id.study_list_item_city);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.title.setText(lists.get(position).get_title());
        viewHolder.desc.setText(lists.get(position).get_desc());
        viewHolder.tv_name.setText(lists.get(position).get_Tdesc());
        viewHolder.tv_city.setText(lists.get(position).get_city());
        if (lists.get(position).get_icon().isEmpty()) {
            viewHolder.iv_myexam.setImageResource(R.color.text_white);

        }else{
            viewHolder.iv_myexam.setDefaultImageResId(R.color.text_white);
            viewHolder.iv_myexam.setErrorImageResId(R.color.text_white);
            viewHolder.iv_myexam.setImageUrl(lists.get(position).get_icon(), imageLoader);


           // MyNet.setNetImage(context, viewHolder.iv_myexam, lists.get(position).get_icon());

        }
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (StudyFragment.curPage == StudyFragment.Page.StudyWhere) {
                    Intent intent = new Intent();
                    intent.putExtra("id", lists.get(position).get_id());
                    intent.setClass(context, StudyClubDetailActivity.class);
                    context.startActivity(intent);
                } else {
                    Intent intent = new Intent();
                    intent.putExtra("id", lists.get(position).get_id());
                    intent.setClass(context, MidiManDetailsActivity.class);
                    context.startActivity(intent);
                }

            }
        });

        return convertView;
    }

    static class ViewHolder {
        TextView title;
        TextView desc, tv_name,tv_city;
        NetworkImageView iv_myexam;
    }

}
